﻿Public Class xtf_ABMgrupo

    Dim ulg_row As DataRow
    Dim obj_Grupo As cls_grupo_ = Nothing

    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()
        CargarModulo_()
    End Sub

    Private Sub CargarModulo_()
        utt_modulos.Nodes.Clear()
        Dim Grupo As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        Dim SubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        Dim STRgrupo As String = ""
        Dim STRsubGrupo As String = ""

        Dim ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        Dim ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        Dim ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        utt_modulos.BeginUpdate()
        Dim parentForRootNodes As DevExpress.XtraTreeList.Nodes.TreeListNode = Nothing
        Dim obj_modulo_ As New cls_modulo_
        Dim obj_config_modulo As New cls_config_modulo_
        Dim bol_ACTIVO As Boolean = False
        If obj_modulo_.CargarTodosOrdenado Then
            For Each modulo As cls_modulo_ In obj_modulo_.Items
                If STRgrupo <> modulo.grupo Then
                    If obj_config_modulo.Buscar(modulo.idmodulo) Then
                        If obj_config_modulo.activo Then
                            ndoGrupo = utt_modulos.AppendNode(New Object() {Nothing, modulo.grupo}, parentForRootNodes)
                            STRgrupo = modulo.grupo
                        End If
                    End If
                End If
                If STRsubGrupo <> modulo.subgrupo Then
                    If obj_config_modulo.Buscar(modulo.idmodulo) Then
                        If obj_config_modulo.activo Then
                            bol_ACTIVO = True
                            ndoSubGrupo = utt_modulos.AppendNode(New Object() {Nothing, modulo.subgrupo}, ndoGrupo)
                            STRsubGrupo = modulo.subgrupo
                        Else
                            bol_ACTIVO = False
                        End If
                    Else
                        bol_ACTIVO = False
                    End If
                End If
                If bol_ACTIVO Then
                    ndoItem = utt_modulos.AppendNode(New Object() {modulo.idmodulo, modulo.modulo}, ndoSubGrupo)
                End If
            Next
        End If
        utt_modulos.EndUpdate()
    End Sub

    Private Sub tre_cuentas_AfterCheckNode(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.NodeEventArgs) Handles utt_modulos.AfterCheckNode
        utt_modulos.BeginUpdate()
        For Each nodo As DevExpress.XtraTreeList.Nodes.TreeListNode In e.Node.Nodes
            nodo.CheckState = e.Node.CheckState
            For Each nodo2 As DevExpress.XtraTreeList.Nodes.TreeListNode In nodo.Nodes
                nodo2.CheckState = e.Node.CheckState
                For Each nodo3 As DevExpress.XtraTreeList.Nodes.TreeListNode In nodo2.Nodes
                    nodo3.CheckState = e.Node.CheckState
                Next
            Next
        Next
        utt_modulos.EndUpdate()
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_Grupo = New cls_grupo_
            btn_Registro.Enabled = False
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef Grupo As cls_grupo_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_Grupo) Then
            obj_Grupo = New cls_Grupo_
        End If
        obj_Grupo = Grupo
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True
        utt_modulos.BeginUpdate()
        For Each ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In utt_modulos.Nodes
            Dim ndoSubGrupoCHK As Integer = 0
            For Each ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoGrupo.Nodes
                Dim ndoItemCHK As Integer = 0
                For Each ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoSubGrupo.Nodes
                    Dim moduloHabilitado As New cls_modulohabilitado_
                    If moduloHabilitado.BuscarXidmoduloHabilitadoXidgrupo(ndoItem("IDModulo"), obj_Grupo.idgrupo) Then
                        ndoItem.CheckState = CheckState.Checked
                        ndoItemCHK += 1
                    End If
                Next
                If ndoItemCHK = ndoSubGrupo.Nodes.Count Then
                    ndoSubGrupo.CheckState = CheckState.Checked
                    ndoSubGrupoCHK += 1
                ElseIf ndoItemCHK <> 0 Then
                    ndoSubGrupo.CheckState = CheckState.Checked
                    ndoSubGrupoCHK += 1
                End If
            Next
            If ndoSubGrupoCHK = ndoGrupo.Nodes.Count Then
                ndoGrupo.CheckState = CheckState.Checked
            ElseIf ndoSubGrupoCHK <> 0 Then
                ndoGrupo.CheckState = CheckState.Checked
            End If
        Next
        utt_modulos.EndUpdate()
        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_Grupo.ToString
        txt_ID.Text = obj_Grupo.idGrupo
        txt_Nombre.Text = obj_Grupo.nombre

        btn_Registro.Enabled = True

        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_Grupo.nombre = txt_Nombre.Text
            If txt_ID.Text = "" Then
                obj_Grupo.Nuevo(True)

                'Guardo Permisos---------------------------------------------------------------------------------
                For Each ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In utt_modulos.Nodes
                    For Each ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoGrupo.Nodes
                        For Each ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoSubGrupo.Nodes
                            Dim moduloHabilitado As New cls_modulohabilitado_
                            If ndoItem.CheckState Then 'Habilitado
                                If Not moduloHabilitado.BuscarXidmoduloHabilitadoXidgrupo(ndoItem("IDModulo"), obj_Grupo.idgrupo) Then
                                    moduloHabilitado.idmodulo = ndoItem("IDModulo")
                                    moduloHabilitado.idgrupo = obj_Grupo.idgrupo
                                    moduloHabilitado.Nuevo()
                                End If
                            Else 'Deshabilitado
                                If moduloHabilitado.BuscarXidmoduloHabilitadoXidgrupo(ndoItem("IDModulo"), obj_Grupo.idgrupo) Then
                                    moduloHabilitado.Eliminar(moduloHabilitado.idmodulohabilitado)
                                End If
                            End If
                        Next
                    Next
                Next
                '-----------------------------------------------------------------------------------------------

                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOgrupo_).AgregarItem(obj_Grupo)
                End If
                Modificar(obj_Grupo, frm_listado, ulg_row)
            Else
                obj_Grupo.Modificar()

                'Guardo Permisos---------------------------------------------------------------------------------
                For Each ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In utt_modulos.Nodes
                    For Each ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoGrupo.Nodes
                        For Each ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoSubGrupo.Nodes
                            Dim moduloHabilitado As New cls_modulohabilitado_
                            If ndoItem.CheckState Then 'Habilitado
                                If Not moduloHabilitado.BuscarXidmoduloHabilitadoXidgrupo(ndoItem("IDModulo"), obj_Grupo.idgrupo) Then
                                    moduloHabilitado.idmodulo = ndoItem("IDModulo")
                                    moduloHabilitado.idgrupo = obj_Grupo.idgrupo
                                    moduloHabilitado.Nuevo()
                                End If
                            Else 'Deshabilitado
                                If moduloHabilitado.BuscarXidmoduloHabilitadoXidgrupo(ndoItem("IDModulo"), obj_Grupo.idgrupo) Then
                                    moduloHabilitado.Eliminar(moduloHabilitado.idmodulohabilitado)
                                End If
                            End If
                        Next
                    Next
                Next
                '-----------------------------------------------------------------------------------------------

                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOgrupo_).ModificarItem(obj_Grupo, ulg_row)
                End If
                Modificar(obj_Grupo, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_Grupo.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_Grupo.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        For Each ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In utt_modulos.Nodes
            ndoGrupo.CheckState = CheckState.Checked
            For Each ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoGrupo.Nodes
                ndoSubGrupo.CheckState = CheckState.Checked
                For Each ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoSubGrupo.Nodes
                    ndoItem.CheckState = CheckState.Checked
                Next
            Next
        Next
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        For Each ndoGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In utt_modulos.Nodes
            ndoGrupo.CheckState = CheckState.Unchecked
            For Each ndoSubGrupo As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoGrupo.Nodes
                ndoSubGrupo.CheckState = CheckState.Unchecked
                For Each ndoItem As DevExpress.XtraTreeList.Nodes.TreeListNode In ndoSubGrupo.Nodes
                    ndoItem.CheckState = CheckState.Unchecked
                Next
            Next
        Next
    End Sub
End Class